import { DependencyContainer, toSuccess, useEntityManager, useRedis } from '@sker/core';
import { useData } from '@sker/runtime';
import dayjs from 'dayjs';
import { z } from 'zod';
export function getDefaultCount(type: string): number {
    switch (type) {
        case '口令':
            return 80
        case '粉丝包':
            return 2;
        case '快币':
            return 100;
        case '分享':
            return 2;
        case '穿云箭':
            return 200;
        default:
            return 1000;
    }
}
export default async (injector: DependencyContainer) => {
    const input = z.object({
        type: z.optional(z.string()),
        uuid: z.string(),
        count: z.optional(z.any())
    })
    const body = input.parse(useData())
    body.type = body.type || `口令`
    body.count = body.count ? parseInt(`${body.count}`) : getDefaultCount(body.type)
    if (body.type === '口令') {
        body.count = 60;
    }
    const list = await useEntityManager(async m => {
        // 获取房间信息
        let allList: any[] = [];
        await useRedis(7, async r => {
            const rooms = await r.keys('rooms*')
            const list = await r.mGet(rooms)
            try {
                allList = list.map(it => JSON.parse(it)).map(it => {
                    if (!it.reds) return;
                    const red = it.reds.find(red => red.type === body.type && red.count >= body.count)
                    if (!red) return;
                    return {
                        count: red.count,
                        id: red.id,
                        link: it.link,
                        open_time: it.open_time,
                        room_id: it.id,
                        user_id: it.user_id,
                        name: it.name,
                        online: it.online,
                    }
                }).filter(it => {
                    if (!it) return false;
                    const endDate = dayjs().add(60 * 5, 'second').toDate().getTime()
                    const openTime = dayjs(it.open_time, 'YYYY-MM-DD HH:mm:ss').toDate().getTime()
                    if (openTime > endDate) return false;
                    return true;
                })
            } catch (e) {
                console.error(e)
            }
        });
        if (allList.length > 0) {
            const joinCounts = []
            const filterList = [...allList.filter(it => {
                const item = joinCounts.find(c => c?.room_id === it?.room_id)
                if (!item) return true;
                return item.count < 7;
            }).map(it => {
                it.money_v = parseInt(`${100 * it.count * Math.sqrt(it.count) / it.online}`) / 100;
                return it;
            }).sort((a, b) => {
                const countDec = b.count - a.count;
                if (countDec === 0) {
                    return b.money_v - a.money_v;
                }
                return countDec;
            })];
            return filterList;
        }
        return [];
    })
    return toSuccess(list)
}
